package com.yz.lzq.liwushuo.model;

import com.yz.lzq.liwushuo.bean.GiftgoryTop100Bean;
import com.yz.lzq.liwushuo.interfaces.GiftTop100;
import com.yz.lzq.liwushuo.util.UrlConfig;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by AdminJia on 2016/12/12.
 */

public class GidftTop100Biz implements IGidftTop100Biz {

    @Override
    public void getData(final OnGiftTop100GetDataListener listener, int offset) {

            Retrofit retrofit = new Retrofit.Builder().baseUrl(UrlConfig.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create()).build();
            GiftTop100 giftTop100 = retrofit.create(GiftTop100.class);
            Call<GiftgoryTop100Bean> call = giftTop100.getData(offset);
            call.enqueue(new Callback<GiftgoryTop100Bean>() {
                @Override
                public void onResponse(Call<GiftgoryTop100Bean> call,
                                       Response<GiftgoryTop100Bean> response) {

                    GiftgoryTop100Bean body = response.body();
                    GiftgoryTop100Bean.DataBean data = body.getData();

                    String imgUrl = data.getCover_webp();
                    List<GiftgoryTop100Bean.DataBean.ItemsBean> items = data.getItems();
                    listener.getDataSuccess(items, imgUrl);
                }

                @Override
                public void onFailure(Call<GiftgoryTop100Bean> call, Throwable t) {
                    listener.getDataFaile(t);
                }
            });


    }

    @Override
    public void getMoreData(int offset, final OnGiftTop100GetDataListener listener) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(UrlConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        GiftTop100 giftTop100 = retrofit.create(GiftTop100.class);
        Call<GiftgoryTop100Bean> call = giftTop100.getData(offset);
        call.enqueue(new Callback<GiftgoryTop100Bean>() {
            @Override
            public void onResponse(Call<GiftgoryTop100Bean> call, Response<GiftgoryTop100Bean>
                    response) {

                GiftgoryTop100Bean body = response.body();
                GiftgoryTop100Bean.DataBean data = body.getData();
                String imgUrl = data.getCover_webp();
                List<GiftgoryTop100Bean.DataBean.ItemsBean> items = data.getItems();
                listener.getDataSuccess(items, imgUrl);
            }

            @Override
            public void onFailure(Call<GiftgoryTop100Bean> call, Throwable t) {
                listener.getDataFaile(t);
            }
        });
    }
}
